package com.example.dormitorysystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dormitorysystem.entity.Beds;
import com.example.dormitorysystem.entity.Buildings;
import com.example.dormitorysystem.entity.Rooms;
import com.example.dormitorysystem.mapper.BedsMapper;
import com.example.dormitorysystem.service.BedsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dormitorysystem.service.BuildingsService;
import com.example.dormitorysystem.service.RoomsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吉吉王国组
 * @since 2022-11-26
 */
@Service
public class BedsServiceImpl extends ServiceImpl<BedsMapper, Beds> implements BedsService {

    @Override
    public List<Beds> getBedsByRoomId(Integer roomId) {
        QueryWrapper<Beds> bedsQueryWrapper = new QueryWrapper<>();
        bedsQueryWrapper.eq("room_id",roomId);
        List<Beds> bedsList = baseMapper.selectList(bedsQueryWrapper);
        return bedsList;
    }

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private BuildingsService buildingsService;

    @Autowired
    private RoomsService roomsService;



    /**
     * 初始化
     * 启动应用时自动统计宿舍楼内剩余床位数
     * 将数据存入到Redis中
     */
    @PostConstruct
    @Override
    public void init() {
        //需要分别统计男生女生的四人间数量和六人间数量
        //当前寝室可能是完整的寝室，也可能是不完整的寝室
        //需要统计当前寝室的情况，然后再进行匹配
        //五个人的寝室当做四人寝



        List<Buildings> buildingList = buildingsService.getBuildingList();
        for (Buildings building : buildingList) {
            //寝室楼可以有混合的情况

            Integer buildingId = building.getId();
            //统计所有的四人间和六人间
            //后面的数据就下一次再定

            //统计每一个楼的四人间和六人间
            int male_four_man_room = 0;
            int male_six_man_room = 0;
            int female_four_man_room = 0;
            int female_six_man_room = 0;

            //对于不能整数匹配的情况，可以随机分配
            int male_random_num = 0;
            int female_random_num = 0;
            List<Rooms> rooms = roomsService.getRoomListByBuildingId(buildingId);
            for (Rooms room : rooms) {
                //这里有性别信息
                Integer gender = room.getGender();
                //先统计位置数
                int bedsCount = roomsService.getBedsCount(room.getId());
                if (bedsCount==0) continue;
                //男生寝室
                if (gender == 1){
                    if (bedsCount < 4){
                        male_random_num += bedsCount;
                    }else if (bedsCount == 4){
                        male_four_man_room += 1;
                    }else if (bedsCount == 5){
                        male_random_num += 1;
                        male_four_man_room += 1;
                    }else if (bedsCount == 6){
                        male_six_man_room += 1;
                    }
                }else if (gender == 0){
                    if (bedsCount < 4){
                        female_random_num += bedsCount;
                    }else if (bedsCount == 4){
                        female_four_man_room += 1;
                    }else if (bedsCount == 5){
                        female_random_num += 1;
                        female_four_man_room += 1;
                    }else if (bedsCount == 6){
                        female_six_man_room += 1;
                   }
                }
            }
            stringRedisTemplate.opsForValue().set(building.toString()+"male_four_man_room", male_four_man_room+"");
            stringRedisTemplate.opsForValue().set(building.toString()+"male_six_man_room", male_six_man_room+"");
            stringRedisTemplate.opsForValue().set(building.toString()+"female_four_man_room", female_four_man_room+"");
            stringRedisTemplate.opsForValue().set(building.toString()+"female_six_man_room", female_six_man_room+"");
            stringRedisTemplate.opsForValue().set(building.toString()+"male_random_num", male_random_num+"");
            stringRedisTemplate.opsForValue().set(building.toString()+"female_random_num", female_random_num+"");


            }
        }
    }

